package cn.tedu._05article.base.filters;

import cn.tedu._05article.pojo.vo.UserVO;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
@WebFilter(filterName = "LoginCheckFilter",urlPatterns ={"/v1/article/*"})
@Slf4j
public class LoginCheckFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse response1 = (HttpServletResponse) response;
        HttpSession session = request1.getSession();
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO != null){
            //
            //
            chain.doFilter(request1, response1);
        }else{
            //未登录，重定向到登录页面
            response1.sendRedirect("/login.html");
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
